package com.gouqi.bookdemobackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.bookdemobackend.common.BaseResponse;
import com.gouqi.bookdemobackend.common.ErrorCode;
import com.gouqi.bookdemobackend.common.ResultUtils;
import com.gouqi.bookdemobackend.exception.BusinessException;
import com.gouqi.bookdemobackend.model.dto.BookQueryRequest;
import com.gouqi.bookdemobackend.model.dto.BookUpdateRequest;
import com.gouqi.bookdemobackend.model.entity.Book;
import com.gouqi.bookdemobackend.model.entity.User;
import com.gouqi.bookdemobackend.model.vo.BookVO;
import com.gouqi.bookdemobackend.service.BookService;
import com.gouqi.bookdemobackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * 书籍接口
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class bookController {

    @Resource
    private BookService bookService;

    @Resource
    private UserService userService;

    /**
     * 更新（仅管理员）
     *
     * @param bookUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateBook(@RequestBody BookUpdateRequest bookUpdateRequest, HttpServletRequest request) {
        if (bookUpdateRequest == null || bookUpdateRequest.getId().length() == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Book book = new Book();
        BeanUtils.copyProperties(bookUpdateRequest, book);
        // 参数校验
        User user = userService.getLoginUser(request);
        if (user == null || user.getId() == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        String id = bookUpdateRequest.getId();
        // 判断是否存在
        Book oldBook = bookService.getById(id);
        if (oldBook == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        if (Objects.equals(oldBook.getStatus(), book.getStatus())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "书籍已被借阅或归还");
        } else if (book.getStatus() == 1) {
            book.setBorrowTime(new Date());
            book.setReturnTime(null);
        } else if (book.getStatus() == 2) {
            book.setReturnTime(new Date());
            book.setBorrowTime(null);
        }
        boolean result = bookService.updateById(book);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param bookQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<BookVO>> listBookVOByPage(@RequestBody BookQueryRequest bookQueryRequest,
                                                       HttpServletRequest request) {
        long current = bookQueryRequest.getCurrent();
        long size = bookQueryRequest.getPageSize();
        Page<Book> bookPage = bookService.page(new Page<>(current, size),
                bookService.getQueryWrapper(bookQueryRequest));
        return ResultUtils.success(bookService.getBookVOPage(bookPage, request));
    }
}
